package com.bmf.module.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.module.rbac.entity.RbacMenu;
import com.bmf.module.rbac.entity.RbacMenuRole;
import com.bmf.module.rbac.entity.RbacUserRole;
import com.bmf.module.rbac.mapper.RbacMenuMapper;
import com.bmf.module.rbac.service.RbacMenuRoleService;
import com.bmf.module.rbac.service.RbacMenuService;
import com.bmf.module.rbac.service.RbacUserRoleService;
import com.bmf.module.rbac.vo.RbacMenuCreateVo;
import com.bmf.module.rbac.vo.RbacMenuResultVo;
import com.bmf.module.rbac.vo.RbacMenuUpdateVo;
import com.bmf.utils.BeanCopyUtils;
import com.bmf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (RbacMenu)表服务实现类
 *
 * @author makejava
 * @since 2022-12-27 15:33:18
 */
@Service("rbacMenuService")
public class RbacMenuServiceImpl extends ServiceImpl<RbacMenuMapper, RbacMenu> implements RbacMenuService {

    @Autowired
    private RbacUserRoleService rbacUserRoleService;

    @Autowired
    private RbacMenuRoleService rbacMenuRoleService;

    @Autowired
    private RbacMenuService rbacMenuService;

    @Override
    public List<RbacMenuResultVo> selectUserAll() {
        LambdaQueryWrapper<RbacUserRole> wrapper = new LambdaQueryWrapper();
        wrapper.eq(RbacUserRole::getUserid, SecurityUtils.getUserId());
        List<RbacUserRole> list = rbacUserRoleService.list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<Integer> roleids = list.stream().map(RbacUserRole::getRoleid).collect(Collectors.toList());
        LambdaQueryWrapper<RbacMenuRole> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(RbacMenuRole::getRoleid, roleids);
        List<RbacMenuRole> list1 = rbacMenuRoleService.list(wrapper1);
        if(CollUtil.isEmpty(list1)){
            return null;
        }
        List<Integer> collect = list1.stream().map(RbacMenuRole::getMenuid).collect(Collectors.toList());
        List<RbacMenu> rbacMenus = rbacMenuService.listByIds(collect);
        if(CollUtil.isEmpty(rbacMenus)){
            return null;
        }
        return arrangementRbacMenu(rbacMenus);
    }

    public List<RbacMenuResultVo> arrangementRbacMenu(List<RbacMenu> rbacMenus){
        List<RbacMenu> pMenu = rbacMenus.stream().filter(s -> s.getPid() == 0).collect(Collectors.toList());
        if(CollUtil.isEmpty(pMenu)){
            return null;
        }
        List<RbacMenuResultVo> rbacMenuResultVos = BeanCopyUtils.copyBeanList(pMenu, RbacMenuResultVo.class);
        rbacMenuResultVos.forEach(s-> getChildrens(rbacMenus, s));
        return rbacMenuResultVos;
    }

    private void getChildrens(List<RbacMenu> rbacMenus, RbacMenuResultVo rbacMenuResultVo) {
        List<RbacMenuResultVo> collect = rbacMenus.stream().filter(s -> s.getPid().equals(rbacMenuResultVo.getId())).map(s -> BeanCopyUtils.copyBean(s, RbacMenuResultVo.class))
                .collect(Collectors.toList());
        if(CollUtil.isNotEmpty(collect)){
            collect.forEach(s-> getChildrens(rbacMenus,s));
        }
        rbacMenuResultVo.setChildrens(collect);
    }

    @Override
    public Integer insert(RbacMenuCreateVo rbacMenuCreateVo) {
        RbacMenu rbacMenu = BeanCopyUtils.copyBean(rbacMenuCreateVo, RbacMenu.class);
        save(rbacMenu);
        return rbacMenu.getId();
    }

    @Override
    public void update(RbacMenuUpdateVo rbacMenuUpdateVo) {
        RbacMenu rbacMenu = BeanCopyUtils.copyBean(rbacMenuUpdateVo, RbacMenu.class);
        updateById(rbacMenu);
    }

    @Override
    public void delete(List<Integer> ids) {
        removeByIds(ids);
    }

    @Override
    public List<RbacMenuResultVo> selectAll() {
        List<RbacMenu> list = list();
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return arrangementRbacMenu(list);
    }

    @Override
    public RbacMenuResultVo selectOne(Serializable id) {
        RbacMenu byId = getById(id);
        if(ObjectUtil.isEmpty(byId)){
            return null;
        }
        return BeanCopyUtils.copyBean(byId, RbacMenuResultVo.class);
    }
}
